//
//  GetDeviceInfoDetailViewController.m
//  Rail
//
//  Created by hychen on 2018/7/22.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "GetDeviceInfoDetailViewController.h"
#import "ButtonTableViewCell.h"
#import "WarnDetailTableViewCell.h"

@interface GetDeviceInfoDetailViewController ()
@property (nonatomic, strong) UIButton *collectionButton;
@property (nonatomic, assign) BOOL isLoading;
@end

@implementation GetDeviceInfoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"设备信息"];
    
    self.collectionButton = [self.navigationItem addRightNavItem:self normalImage:[UIImage imageNamed:@"app_title_bar_back_n_ic"] highlightedImage:[UIImage imageNamed:@"app_title_bar_back_p_ic"] selector:@selector(clickCollectionButton:)];
    [self.collectionButton setImage:[UIImage imageNamed:@"app_title_bar_back_n_ic"] forState:UIControlStateNormal];
    [self.collectionButton setImage:[UIImage imageNamed:@"app_title_bar_back_p_ic"] forState:UIControlStateSelected];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndexSafe:indexPath.row];
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return [UITableViewCell new];
    }
    BMWeakSelf
    if ([[dict objectForKeySafe:@"type"] isEqualToString:@"desc"]) {
        WarnDetailTableViewCell *cell = [WarnDetailTableViewCell cellWithTableView:tableView class:[WarnDetailTableViewCell class]];
        cell.contentLabel.textAlignment = NSTextAlignmentLeft;
        cell.titleLabel.text = [dict objectForKeySafe:@"title"];
        cell.contentLabel.text = [dict objectForKeySafe:@"content"];
        return cell;
    }
    else if ([[dict objectForKeySafe:@"type"] isEqualToString:@"button"]) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.buttonTitle = [dict objectForKeySafe:@"title"];
        [cell setBlockClickEventButton:^{
            if ([[dict objectForKeySafe:@"content"] isEqualToString:@"rent"]) {
                [weakSelf requestRentData];
            }
            else if ([[dict objectForKeySafe:@"content"] isEqualToString:@"revert"]) {
                [weakSelf requestRevertData];
            }
        }];
        return cell;
    }
    else {
        return [UITableViewCell new];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = [self.dataArray objectAtIndexSafe:indexPath.row];
    if (![dict isKindOfClass:[NSDictionary class]]) {
        return 0;
    }
    if ([[dict objectForKey:@"type"] isEqualToString:@"desc"]) {
        return 50;
    }
    else if ([[dict objectForKey:@"type"] isEqualToString:@"button"]) {
        return 200;
    }
    return 0;
}

///领用设备
- (void)requestRentData {
    
}

///归还设备
- (void)requestRevertData {
    
}

#pragma mark - 收藏
- (void)addFactoryCollection {
    if (self.isLoading) {
        return;
    }
#warning favid != 0
    if (NO) {
        return;
    }
    self.isLoading = YES;
    Show_Msg(@"收藏中")
    BMWeakSelf
#warning 赋值
    [HttpRequest addFavWithLineId:@"" areaId:@"" deviceId:@"" success:^(id object) {
        weakSelf.collectionButton.selected = YES;
        Show_Msg_Success(@"收藏成功")
        weakSelf.isLoading = NO;
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        weakSelf.isLoading = NO;
    }];
}

- (void)cancelFactoryCollection {
    if (self.isLoading) {
        return;
    }
    self.isLoading = YES;
    Show_Msg(@"取消收藏中")
    BMWeakSelf
#warning 赋值
    [HttpRequest cancelFavWithFavId:nil success:^(id object) {
        weakSelf.collectionButton.selected = NO;
        Show_Msg_Success(@"取消收藏成功")
        weakSelf.isLoading = NO;
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        weakSelf.isLoading = NO;
    }];
}

#pragma mark - getter
- (NSMutableArray *)dataArray {
    return @[
             @{@"type":@"desc",@"title":@"设备名称:",@"content":@"",},
             @{@"type":@"desc",@"title":@"设备类型:",@"content":@"",},
             @{@"type":@"desc",@"title":@"设备状态:",@"content":@"",},
             @{@"type":@"desc",@"title":@"操作人员:",@"content":@"",},
             @{@"type":@"desc",@"title":@"操作时间:",@"content":@"",},
             @{@"type":@"desc",@"title":@"领用人员:",@"content":@"",},
             @{@"type":@"desc",@"title":@"领用时间:",@"content":@"",},
             @{@"type":@"button",@"title":@"归还设备",@"content":@"revert",},
             ].mutableCopy;
    
//    @{@"type":@"button",@"title":@"领用设备:",@"content":@"rent",},
}
@end
